﻿using System;
using Windows.Foundation;

namespace LinkPad.Shapes
{
    public abstract class BaseManagedLine : ManagedShape
    {
        #region Fields

        private double _realX1;
        private double _realY1;
        private double _realX2;
        private double _realY2;

        protected bool RealBoundingBoxInvalid;
        private Rect _realBoundingBox;

        #endregion

        #region Constructors

        protected BaseManagedLine(ShapeManager shapeManager)
            : base(shapeManager)
        {

        }

        #endregion

        #region Properties

        #region ManagedShape members

        public override Rect RealBoundingBox
        {
            get
            { 
                if (RealBoundingBoxInvalid)
                {
                    var minX = Math.Min(RealX1, RealX2);
                    var maxX = Math.Max(RealX1, RealX2);
                    var minY = Math.Min(RealY1, RealY2);
                    var maxY = Math.Max(RealY1, RealY2);
                    _realBoundingBox.X = minX;
                    _realBoundingBox.Y = minY;
                    _realBoundingBox.Width = maxX - minX;
                    _realBoundingBox.Height = maxY - minY;
                    RealBoundingBoxInvalid = false;
                }
                return _realBoundingBox;
            }
        }

        #endregion

        public double RealX1
        {
            get { return _realX1; }
            protected set
            {
                _realX1 = value;
                RealBoundingBoxInvalid = true;
            }
        }

        public double RealY1
        {
            get { return _realY1; }
            protected set
            {
                _realY1 = value;
                RealBoundingBoxInvalid = true;
            }
        }

        public double RealX2
        {
            get { return _realX2; }
            protected set
            {
                _realX2 = value;
                RealBoundingBoxInvalid = true;
            }
        }

        public double RealY2
        {
            get { return _realY2; }
            protected set
            {
                _realY2 = value;
                RealBoundingBoxInvalid = true;
            }
        }

        #endregion
    }
}
